package main

import "fmt"

func main() {
	obj := Constructor()
	obj.AddNum(1)                           // arr = [1]
	fmt.Printf("%#v\n", obj.GetIntervals()) // 返回 [[1, 1]]
	obj.AddNum(3)                           // arr = [1, 3]
	fmt.Printf("%#v\n", obj.GetIntervals()) // 返回 [[1, 1], [3, 3]]
	obj.AddNum(7)                           // arr = [1, 3, 7]
	fmt.Printf("%#v\n", obj.GetIntervals()) // 返回 [[1, 1], [3, 3], [7, 7]]
	obj.AddNum(2)                           // arr = [1, 2, 3, 7]
	fmt.Printf("%#v\n", obj.GetIntervals()) // 返回 [[1, 3], [7, 7]]
	obj.AddNum(6)                           // arr = [1, 2, 3, 6, 7]
	fmt.Printf("%#v\n", obj.GetIntervals()) // 返回 [[1, 3], [6, 7]]

}

type SummaryRanges struct {
	buff []int
}

func Constructor() SummaryRanges {
	return SummaryRanges{
		buff: []int{},
	}
}

func (this *SummaryRanges) AddNum(val int) {
	n := len(this.buff)
	for i := 0; i < n; i++ {
		if this.buff[i] > val {
			this.buff = append(this.buff, 0)
			copy(this.buff[i+1:], this.buff[i:])
			this.buff[i] = val
			return
		}
	}
	this.buff = append(this.buff, val)
}

func (this *SummaryRanges) GetIntervals() (res [][]int) {
	res = make([][]int, 0)
	start := this.buff[0]
	end := this.buff[0]
	res = append(res, make([]int, 2))
	index := 0
	for _, v := range this.buff {
		if v == end || v == end+1 {
			res[index][0] = start
			res[index][1] = v
			end = v
			continue
		}
		index++
		res = append(res, make([]int, 2))
		res[index][0] = v
		res[index][1] = v
		start = v
		end = v
	}
	return
}
